/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package servlets;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author recsko
 */
public class Feldolgoz extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");

        PrintWriter out = response.getWriter();
        RequestDispatcher disp;
        try {

           int sessionszam =0;
           int kukisszam =0;
           int hiddenszam = 0;

           StringBuilder errors = new StringBuilder();
           // Sessionos megoldás -->
             HttpSession session = request.getSession();
             if (session.getAttribute("sessionszam")!=null){
                 sessionszam = ((Integer) session.getAttribute("sessionszam")).intValue();
             }
           // <--SEssionos megoldás

           //Rejtett mezős megoldás -->
           try{
               hiddenszam = Integer.parseInt(request.getParameter("hiddenszam"));
           } catch(NumberFormatException e){
               errors.append("Hibás 'hiddenszám' paraméter!");
     
           }
           // <-- rejtett mezős megoldás

          // Cookies megoldás -->
             Cookie sutik[] =  request.getCookies();
             Cookie szamkuki=null;

             for (int i =0; i<sutik.length;i++){
                 // amit itt kííratok azt a böngészőben is megnézheted, a Mutasd a Kukidat gombra kattintva
                 // ha összeveted láthatod, hogy a serverre is megjön minden kérésnél a böngészőben lévő süti
                 System.out.println(sutik[i].getName()+":  "+sutik[i].getValue());
                 if ("kukisszam".equals(sutik[i].getName())){
                   szamkuki = sutik[i];
                 }
             }


             if (szamkuki!=null) kukisszam= Integer.parseInt(szamkuki.getValue());
             if (szamkuki==null)
                szamkuki = new Cookie("kukisszam", "0" );

          //<-- Sütis megoldás


           String muvelet = (String) request.getParameter("muvelet");
           if (muvelet==null) errors.append("Nem jött művelet");


           if (errors.length()==0){

               System.out.println(muvelet);

              if (muvelet.equals("Csokkent")){
                  sessionszam--;
                  kukisszam--;
                  hiddenszam--;
              }
              if (muvelet.equals("Novell")){
                  sessionszam++;
                  kukisszam++;
                  hiddenszam++;
              }
              if (muvelet.equals("Nullaz")){
                  sessionszam=0;
                  kukisszam=0;
                  hiddenszam=0;
              }
              session.setAttribute("sessionszam",new Integer(sessionszam));
              request.setAttribute("sessionszam", new Integer(sessionszam));
              request.setAttribute("hiddenszam", hiddenszam);
              request.setAttribute("kukisszam", kukisszam);

              szamkuki.setValue(String.valueOf(kukisszam));
              response.addCookie(szamkuki);
              disp = request.getRequestDispatcher("index.jsp");

           } else {
               request.setAttribute("errors",errors.toString());
               disp = request.getRequestDispatcher("error.jsp");
           }

           disp.forward(request, response);
        } finally {
       
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
